package com.feather.common.data.simple;

import java.util.ArrayList;

import com.feather.common.data.DataTable;
import com.feather.common.data.DataTableCollection;

/**
 * @author flogyin
 */
public class SimpleDataTableCollection implements DataTableCollection, java.io.Serializable {
	private static final long		serialVersionUID	= 1L;

	protected ArrayList<DataTable>	collection			= new ArrayList<DataTable>();

	@Override
	public int getCount() {
		return this.collection.size();
	}

	@Override
	public DataTable get(int tableIndex) {
		return this.collection.get(tableIndex);
	}

	@Override
	public DataTable get(String tableName) {
		int tableIndex = indexOf(tableName);
		return tableIndex > -1 ? this.collection.get(tableIndex) : null;
	}

	@Override
	public DataTable add() {
		SimpleDataTable dataTable = new SimpleDataTable();
		this.collection.add(dataTable);
		return dataTable;
	}

	@Override
	public void add(DataTable table) {
		if (table != null)
			this.collection.add(table);
	}

	@Override
	public boolean contains(String tableName) {
		return indexOf(tableName) > -1;
	}

	@Override
	public int indexOf(DataTable table) {
		return table != null ? indexOf(table.getName()) : -1;
	}

	@Override
	public int indexOf(String tableName) {
		if (tableName != null) {
			int count = getCount();
			for (int i = 0; i < count; i++) {
				String temp = this.collection.get(i).getName();
				if (tableName.equalsIgnoreCase(temp))
					return i;
			}
		}
		return -1;
	}

	@Override
	public void remove(DataTable table) {
		if (table != null) {
			int tableIndex = indexOf(table.getName());
			if (tableIndex > -1)
				this.collection.remove(tableIndex);
		}
	}

	@Override
	public void remove(String tableName) {
		int tableIndex = indexOf(tableName);
		if (tableIndex > -1)
			this.collection.remove(tableIndex);
	}

	@Override
	public void removeAt(int tableIndex) {
		this.collection.remove(tableIndex);
	}

	@Override
	public void clear() {
		this.collection.clear();
	}
}
